package com.dkhseep.bigevent.controller;

import com.dkhseep.bigevent.pojo.Result;
import com.dkhseep.bigevent.pojo.User;
import com.dkhseep.bigevent.service.UserService;
import com.dkhseep.bigevent.util.JwtUtil;
import com.dkhseep.bigevent.util.Md5Util;
import com.dkhseep.bigevent.util.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 功能:
 * 作者:Dksheep
 * 日期:2024/9/2920:27
 */
@Validated
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired private UserService userService;
    @Autowired private StringRedisTemplate stringRedisTemplate;
    //用户注册
    @PostMapping("/register")
    public Result rigister(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "\\S{5,16}$") String password){
        User user = userService.findUserByUsername(username);
        System.out.println(user);
        if(user!=null)
            return Result.error("用户名被占用");
        userService.register(username,Md5Util.getMD5String(password));
        return Result.success("注册成功");
    }


    //用户登录
    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "\\S{5,16}$") String password){
        User user = userService.findUserByUsername(username);
        if(user==null){
            return Result.error("用户名错误");
        }
        if(!user.getPassword().equals(Md5Util.getMD5String(password))){
            return Result.error("密码错误");
        }
        String token = userService.gettoken(user);
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set(username,token,1, TimeUnit.HOURS); //存入username-token
        return Result.success(token);
    }

    //用户信息
    @GetMapping("/userInfo")
    public Result<User>GetuserInfo(){
        //Map<String,Object>clamis = JwtUtil.parseToken(token);
        Map<String,Object>clamis = ThreadLocalUtil.get();
        String username = (String) clamis.get("username");
        User user = userService.findUserByUsername(username);
        return Result.success(user);
    }
    //更新用户信息
    @PutMapping("/update")
    public Result updateUserInfo(@RequestBody @Validated User user){
        userService.updateuser(user);
        return Result.success();
    }
    //跟新用户头像
    @PatchMapping("/updateAvatar")
    public Result updateUserAvatar(@RequestParam  @URL String avatarUrl){
        userService.updateUserAvatar(avatarUrl);
        return Result.success();
    }

    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> map,@RequestHeader("Authorization") String token){
        String old_pwd = map.get("old_pwd");
        String new_pwd = map.get("new_pwd");
        String re_pwd = map.get("re_pwd");
        if(old_pwd==null || new_pwd==null || re_pwd==null){
            return Result.error("填写不能为空");
        }
        if(!userService.JudgeOldPassword(old_pwd)){
            return Result.error("旧密码错误");
        }
        if(!userService.JudgeNew_RePassowrd(new_pwd,re_pwd)){
            return Result.error("确认密码错误");
        }
        userService.updateUserPassword(new_pwd);
        userService.deletetoken();
        return Result.success();

    }
}
